$(function(){

		// Location: ATM
        var cache = {};
        $("#province").on('change', function () {

            var pronvince = $(this).val();

            if(typeof cache[pronvince] != "undefined") {
                $('#branch').html('');
                renderOption(cache[pronvince]);
            } else {
                $.ajax({
                    url: '/admin/branches/province/' + pronvince,
                    dataType: 'json'
                }).done(function(data) {
                    if (data.length > 0) {
                        cache[pronvince] = data;
                        renderOption(data);
                        console.log(cache);
                    } else {
                        $('#branch').html('');
                        cache[pronvince] = '';
                    }
                });
            }

        });
        var renderOption = function (data) {
            var options = '';
            if (data.length > 0) {
	            for (var i = 0; i < data.length; i++) {
	                options += '<option value="'+ data[i].id +'">'+ data[i].place.name +'</option>';
	            }
	        }
            $('#branch').html(options);
        }
    });
